<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input @keyup.enter.native="handleFilter" style="width: 200px;" class="filter-item" placeholder="Resource Name" v-model="listQuery.filterItem">
      </el-input>
      <el-button class="filter-item" type="primary"  icon="el-icon-search" @click="handleFilter">search</el-button>
      <el-button class="filter-item" style="margin-left: 10px;" @click="handleCreate" type="primary" icon="el-icon-edit">add</el-button>
    </div>
    <el-table :data="list" v-loading.body="listLoading" element-loading-text="Loading" border fit highlight-current-row>
      <el-table-column align="center" label='Index' width="95">
        <template slot-scope="scope">
          {{ 1 + scope.$index + ((listQuery.page -1 ) * listQuery.limit) }}
        </template>
      </el-table-column>
      <el-table-column label="Resource Name">
        <template slot-scope="scope">
          {{scope.row.resources_name}}
        </template>
      </el-table-column>
      <el-table-column label="Idle Amount" width="200" align="center">
        <template slot-scope="scope">
          <span>{{scope.row.idle_amount}}</span>
        </template>
      </el-table-column>
      <el-table-column label="Total Amount" width="200" align="center">
        <template slot-scope="scope">
          {{scope.row.total_amount}}
        </template>
      </el-table-column>
      <el-table-column align="center" label="Type" width="200">
        <template slot-scope="scope">
          <span>{{scope.row.type}}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="Price" width="110">
        <template slot-scope="scope">
          <span>{{scope.row.price}}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="Actions" label="Actions" width="200">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" @click="handleShowDetail(scope.$index)">usage
          </el-button>
          <el-button type="primary" size="mini" @click="handleUpdate(scope.$index)">update
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <div class="pagination-container">
      <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="listQuery.page" :page-sizes="[10,20,30, 50]" :page-size="listQuery.limit" layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </div>

    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form :rules="rules" ref="dataForm" :model="tempResource" label-position="left" label-width="120px" style='width: 500px; margin-left:30px;'>
        <el-form-item label="Resource Name">
        <el-input disabled v-model="tempResource.resources_name"></el-input>
      </el-form-item>
      <el-form-item label="Type">
        <el-select disabled v-model="tempResource.type" placeholder="please select resource type">
          <el-option v-for="item in  typeList" :key="item.type_name" :label="item.type_name" :value="item.type_name">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="Total Amount">
        <el-input disabled v-model="tempResource.total_amount"></el-input>
      </el-form-item>
      <el-form-item label="Adjust Amount">
        <el-input type="number" v-model="tempResource.adjust_amount"></el-input>
      </el-form-item>
      <el-form-item label="Price">
        <el-input disabled v-model="tempResource.price"></el-input>
      </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="dialogFormVisible = false">Close</el-button>
        <el-button type="primary" @click="handleAdjust">Confirm</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { getResourcesList, adjustTotalAmount } from '@/api/resource'

export default {
  data() {
    return {
      list: null,
      listLoading: true,
      tempResource: {
        resources_name: '',
        type: '',
        total_amount: '',
        price: '',
        unit: '',
        adjust_amount: ''
      },
      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        show: 'Resource Using',
        update: 'Resource Edit'
      },
      typeList: [{ 'type_name': 'Computer' }, { 'type_name': 'PC components' }],
      rules: {
      },
      total: null,
      listQuery: {
        page: 1,
        limit: 10,
        filterItem: null,
        type: null,
        status: null,
        sort: null
      },
      importanceOptions: [1, 2, 3],
      sortOptions: [{ label: 'ID Ascending', key: '+id' }, { label: 'ID Descending', key: '-id' }]
    }
  },
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'gray',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      getResourcesList(this.listQuery).then(response => {
        this.list = response.dataList
        this.total = response.total
        this.listLoading = false
      })
    },
    handleSizeChange(val) {
      this.listQuery.limit = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.listQuery.page = val
      this.getList()
    },
    handleFilter() {
      this.getList()
    },
    handleShowDetail(index) {
      this.$router.push({
        name: 'ResDetailUsage',
        params: {
          'resources_id': this.list[index].resources_id,
          'type': 'showdetail'
        }
      })
    },
    handleUpdate(index) {
      this.tempResource = Object.assign({}, this.list[index])
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    handleCreate() {
      this.$router.push({
        name: 'AddResources',
        params: {
          'resource': {},
          'type': 'create'
        }
      })
      // this.resetTemp()
      // this.dialogStatus = 'create'
      // this.dialogFormVisible = true
      // this.$nextTick(() => {
      //   this.$refs['dataForm'].clearValidate()
      // })
    },
    handleAdjust(){
      adjustTotalAmount(this.tempResource.resources_id, this.tempResource.adjust_amount).then(() => {
        this.$notify({
          title: 'Success',
          message: 'Adjust Success!',
          type: 'success',
          duration: 2000
        })
        this.dialogFormVisible = false
        this.getList()
      })
    }
  }
}
</script>
